<?php
  class LoginService{
  
  	public function validateUser($uname, $pass){
    
			if( strlen(trim($uname)) > 0 && strlen(trim($pass)) > 0){
				//Check user in database
				$pntables = pnDBGetTables();
				$userTable            = $pntables['users'];     
				$userColumn           = $pntables['users_column']; 
			 
				$userArray = array();
	
				$sql = "
								SELECT
									$pntables[users].$userColumn[uid],
									$pntables[users].$userColumn[uname],
									$pntables[users].$userColumn[pass],
									$pntables[users].$userColumn[email],
									$pntables[users].$userColumn[hash_method]
								FROM
									$pntables[users]
								WHERE
										$pntables[users].$userColumn[uname] = '" . pnVarPrepForStore($uname) . "'
								 ";
	
        $column = array('uid', 'uname', 'pass', 'email', 'hash_method');

        $result = DBUtil::executeSQL($sql);
        $userArrayTemp = DBUtil::marshallObjects ($result, $column);

				//echo print_r($userArray); exit;
        //Check encryption type
        $hashArray = array('md5'=> 1, 'sha1'=> 5, 'sha256' => 8);
        $hash = array_search($userArrayTemp[0]['hash_method'], $hashArray); 
        $encryptPass = DataUtil::hash(strtolower($pass), $hash);

				//echo "Input : ".$userArray[0]['pass']."<br>";
				//echo "System : ".$encryptPass."<br>"; exit;
				
        //Check pass word
        
        if(isset($userArrayTemp) && !empty($userArrayTemp) && $userArrayTemp[0]['pass'] == $encryptPass){
        	$userArray = $userArrayTemp[0];	
        	$userArray['validate'] = true;
        	return $userArray;
        }else{
        	$userArray['validate'] = false;        
        	return $userArray;
        }
        
      }//end strlen check
  	
  	}
  	
  	public function getAccess($uid){
    
			if( $uid){
				//Check user in database
				$pntables = pnDBGetTables();
				$layerTable            = $pntables['votemember_layer'];     
				$layerColumn           = $pntables['votemember_layer_column']; 
			 
				$accessArray = array();
	
				$sql = "
								SELECT
									$layerTable.$layerColumn[uid],
									$layerTable.$layerColumn[province_access],
									$layerTable.$layerColumn[district_access],
									$layerTable.$layerColumn[tambon_access]
								FROM
									$layerTable 
								WHERE
										 $layerTable.$layerColumn[uid] = $uid
								 ";
	
        $column = array('uid', 'province_access', 'district_access', 'tambon_access');

        $result = DBUtil::executeSQL($sql);
        $accessArrayTemp = DBUtil::marshallObjects ($result, $column);

				$accessArray = array();
				$accessArray['uid']             = $accessArrayTemp[0]['uid'];
				$accessArray['province_access'] = implode(',', unserialize($accessArrayTemp[0]['province_access']));
				$accessArray['district_access'] = implode(',', unserialize($accessArrayTemp[0]['district_access']));
				$accessArray['tambon_access']   = implode(',', unserialize($accessArrayTemp[0]['tambon_access']));								
				
				
				
			  return $accessArray;
        
      }//end check uid
  	
  	}
  	
  }


?>